package com.sec.etech.common.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sec.etech.common.util.DataTable;
import com.sec.etech.sys.msg.EtechMailService;
import com.sec.etech.sys.msg.model.EtechEmailMessage;
import org.openbpm.base.core.util.AppUtil;
import org.openbpm.base.core.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 兼容C#版邮件帮助类
 */

@lombok.Data
@lombok.extern.slf4j.Slf4j
public class Mail {

    private DataTable _recipients;
    private String _recipient;
    private String _subject;
    private String _body;
    private String _fileName;
    private boolean _secret;
    private DataTable _ccrecipients;
    private ArrayList<String> _fileNames;


    public Mail(){

    }

    public Mail(Long recipientUid, String subject, String body)
    {
        this(String.valueOf(recipientUid), subject, body, (String) null, false, null);
    }
    public Mail(String recipientUid, String subject, String body)
    {
        this(recipientUid, subject, body, (String) null, false, null);
    }

    public Mail(Long[] recipientUids, String subject, String body)
    {
        DataTable recipients = new DataTable();
        if(recipientUids!=null){
            for(Long uid: recipientUids){
                if(uid==null||uid==0){
                    continue;
                }
                recipients.addNewRow("uid", uid);
            }
        }
        this._recipients = recipients;
        this._subject = subject;
        this._body = body;
    }

    public Mail(List<String> recipientUids, String subject, String body)
    {
        DataTable recipients = new DataTable();
        if (recipientUids != null) {
            for (String uid : recipientUids) {
                if (StrUtil.isBlank(uid) || uid.equals("0")) {
                    continue;
                }
                recipients.addNewRow("uid", uid);
            }
        }
        this._recipients = recipients;
        this._subject = subject;
        this._body = body;
    }

    public Mail(String[] recipientUids, String subject, String body)
    {
        DataTable recipients = new DataTable();
        if(recipientUids!=null){
            for(String uid: recipientUids){
                recipients.addNewRow("uid", uid);
            }
        }
        this._recipients = recipients;
        this._subject = subject;
        this._body = body;
    }

    public Mail(String recipientUid, String subject, String body, String fileName)
    {
        this(recipientUid, subject, body, fileName, false, null);
    }

    public Mail(String recipientUid, String subject, String body, String fileName, boolean sectet, DataTable ccrecipients)
    {
        this._recipient = recipientUid;
        this._subject = subject;
        this._body = body;
        this._fileName = fileName;
        this._secret = sectet;
        this._ccrecipients = ccrecipients;
    }

    public Mail(String recipientUid, String subject, String body,  ArrayList<String> fileNames, boolean sectet, DataTable ccrecipients)
    {
        this._recipient = recipientUid;
        this._subject = subject;
        this._body = body;
        this._fileNames = fileNames;
        this._secret = sectet;
        this._ccrecipients = ccrecipients;
    }

    public Mail(List<String> recipientUids, String subject, String body,  ArrayList<String> fileNames, boolean sectet, List<String> ccrecipients)
    {
        DataTable recipients = new DataTable();
        if (recipientUids != null) {
            for (String uid : recipientUids) {
                recipients.addNewRow("uid", uid);
            }
        }
        this._recipients = recipients;
        this._subject = subject;
        this._body = body;
        this._fileNames = fileNames;
        this._secret = sectet;
        DataTable ccrecipient = new DataTable();
        if (ccrecipients != null) {
            for (String uid : ccrecipients) {
                ccrecipient.addNewRow("uid", uid);
            }
        }
        this._ccrecipients = ccrecipient;
    }

    public Mail(DataTable recipients, String subject, String body)
    {
        this(recipients, subject, body, null, false, null);
    }

    public Mail(DataTable recipients, String subject, String body, int nothing, java.util.ArrayList<String> fileNames)
    {
        this(recipients, subject, body, fileNames, false, null);
    }

    public Mail(DataTable recipients, String subject, String body, ArrayList<String> fileNames, boolean sectet, DataTable ccrecipients)
    {
        this._recipients = recipients;
        this._subject = subject;
        this._body = body;
        this._fileNames = fileNames;
        this._secret = sectet;
        this._ccrecipients = ccrecipients;
    }

    /** 发送电子邮件。

     @return 是否发送成功。
     */
    public final void SendEmail(){
        try{
            doSendMail(this);
        }catch (Exception e){
            log.error("SendEmail error: " +e.getMessage(), e);
            e.printStackTrace();
        }

    }
    protected void doSendMail(Mail mail){
        EtechEmailMessage msg = new EtechEmailMessage();

        msg.setSubject(mail._subject);
        // 内容
//        String bodyStr = "<!DOCTYPE html PUBLIC ' -//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
//        bodyStr += "<html xmlns='http://www.w3.org/1999/xhtml'>";
//        bodyStr += "<head>";
//        bodyStr += "<title>" + this._subject.replace(" ", "") + " | E-Tech 技术工作平台</title>";
//        bodyStr += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
//        bodyStr += "</head>";
//        bodyStr += "<body>";
//        bodyStr += this._body;
//        bodyStr += "</body>";
//        bodyStr += "</html> ";
//        msg.setHtmlContent(bodyStr);
        msg.setHtmlContent(this._body);
        // 附件
        if(this._fileNames!=null && this._fileNames.size()>0){
            msg.setFiles(String.join(",", this._fileNames));
        }else if(StringUtil.isNotEmpty(this._fileName)){
            msg.setFiles(this._fileName);
        }
        // 收件人
        List<String> toList = new ArrayList<String>();
        if (this._recipient != null){
            toList.addAll(Arrays.asList(this._recipient.split(",")));
        }
        if (this._recipients != null && this._recipients.Rows!=null)
        {
            String email = "";
            String uid = "";
            for (int k = 0; k < this._recipients.Rows.size(); k++)
            {
                if(this._recipients.getRows().get(k).containsKey("email")){
                    email = String.valueOf(this._recipients.getRows().get(k).get("email"));
                    if(StringUtil.isNotEmpty(email)){
                        toList.add(email);
                    }
                }else if(this._recipients.getRows().get(k).containsKey("uid")){
                    uid = String.valueOf(this._recipients.getRows().get(k).get("uid"));
                    if(StringUtil.isNotEmpty(uid)){
                        toList.add(uid);
                    }
                }
            };
        };
        if(CollectionUtil.isNotEmpty(toList)){
            String strTo = StringUtil.join(toList, ",");
            if (!this._secret){
                msg.setTo(strTo);
            }else {
                msg.setBcc(strTo);
            }
        }
        // 抄送人
        List<String> ccList = new ArrayList<String>();
        if (this._ccrecipients != null && this._ccrecipients.Rows!=null)
        {
            String email = "";
            String uid = "";
            for (int k = 0; k < this._ccrecipients.Rows.size(); k++)
            {
                if(this._ccrecipients.getRows().get(k).containsKey("email")){
                    email = String.valueOf(this._ccrecipients.getRows().get(k).get("email"));
                    if(StringUtil.isNotEmpty(email)){
                        ccList.add(email);
                    }
                }else if(this._ccrecipients.getRows().get(k).containsKey("uid")){
                    uid = String.valueOf(this._ccrecipients.getRows().get(k).get("uid"));
                    if(StringUtil.isNotEmpty(uid)){
                        ccList.add(uid);
                    }
                }
            };
        };
        if(CollectionUtil.isNotEmpty(ccList)){
            String strCc = StringUtil.join(ccList, ",");
            if (!this._secret){
                msg.setCc(strCc);
            }else {
                if(CollectionUtil.isNotEmpty(toList)){
                    ccList.addAll(toList);
                }
                strCc = StringUtil.join(ccList, ",");
                msg.setBcc(strCc);
            }
        }
        EtechMailService mailService = getMailService();
        if(StringUtil.isNotEmpty(msg.getTo()) || StringUtil.isNotEmpty(msg.getCc()) || StringUtil.isNotEmpty(msg.getBcc())) {
            mailService.sendMail(msg);
        }
    }

    protected EtechMailService getMailService(){
        return (EtechMailService)AppUtil.getBean("etechMailService");
    }
}
